package net.println.kotlin.chapter9

fun main(args: Array<String>) {
    val a = 4
    val b = 3
    println(maxOf(a, b))

    val complex = Complex(3.0, 4.0)
    val complex2 = Complex(3.0, 5.0)
    println(complex)
    println(maxOf(complex, complex2))

    val aaa = Complex2(3,4)
    val bbb = Complex2(3.0,4.0)
    println(aaa)
    println(bbb)
}

data class Complex(val a: Double, val b: Double) : Comparable<Complex> {
    override fun compareTo(other: Complex): Int {
        return (value() - other.value()).toInt()
    }

    fun value(): Double {
        return a * a + b * b
    }

    override fun toString(): String {
        return "($a + $b i)"
    }

}

//泛型方法、泛型边界
fun <T : Comparable<T>> maxOf(a: T, b: T): T {
    return if (a > b) a else b
}

//泛型类
data class Complex2<T : Number>(val a: T, val b: T) {
    override fun toString(): String {
        return "Complex2(a=$a, b=$b)"
    }
}